package com.avic.rong.course.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.login.LoginPhoneActivity;
import com.avic.rong.course.bean.CompanysBean;
import com.avic.rong.questionOrAnswer.activity.PersonalHomeActivity;
import com.avic.rong.utils.DensityUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.StringUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.coorchice.library.SuperTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Chenjiaxin on 2019/8/11 18:19
 * E-Mail Address：16601203965@163.com
 */
public class CompanysRvAdapter extends RecyclerView.Adapter<CompanysRvAdapter.CompanysViewHolder> {

    private Context context;
    private LayoutInflater inflater;

    private List<CompanysBean.CompanysListBean> dataList = new ArrayList<>();
    private CompanysListener listener;

    public CompanysListener getListener() {
        return listener;
    }


    public List<CompanysBean.CompanysListBean> getDataList() {
        return dataList;
    }

    public void setDataList(List<CompanysBean.CompanysListBean> dataList) {
        this.dataList.addAll(dataList);
        notifyDataSetChanged();
    }

    public void setListener(CompanysListener listener) {
        this.listener = listener;
    }

    public CompanysRvAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public CompanysViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.peixun_findexpert_rv_item, viewGroup, false);
        return new CompanysViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CompanysViewHolder holder, int i) {
        CompanysBean.CompanysListBean bean = dataList.get(i);
        holder.tv_duty.setVisibility(View.GONE);
        Glide.with(context).applyDefaultRequestOptions(RequestOptions.circleCropTransform().error(R.mipmap.deafult_image_header)).load(bean.getCompanyLogo()).into(holder.img_header);
        holder.tv_name.setText(bean.getCompany());
        holder.tv_duty.setVisibility(View.GONE);
        if (!StringUtils.isEmpty(bean.getCompany())) {
            holder.tv_company_name.setVisibility(View.VISIBLE);
            holder.tv_company_name.setText("认证信息：" + bean.getCompany());
        }else{
            holder.tv_company_name.setVisibility(View.VISIBLE);
            holder.tv_company_name.setText("认证信息：");
        }

        if (!StringUtils.isEmpty(bean.getMainFields())) {
            holder.tv_special_auth.setVisibility(View.VISIBLE);
            holder.tv_special_auth.setText("简介：" + bean.getCompanyDesc());
        }else{
            holder.tv_special_auth.setVisibility(View.VISIBLE);
            holder.tv_special_auth.setText("简介：");
        }
        boolean isFollow = bean.isIsFollow();
        if (isFollow) {
            holder.tvFollow.setText("已关注");
            holder.tvFollow.setGravity(Gravity.CENTER);
            holder.tvFollow.setPadding(0, 0, 0, 0);
            holder.tvFollow.setSelected(true);
            holder.tvFollow.setTextColor(ContextCompat.getColor(context, R.color.color_98c727));
        } else {
            holder.tvFollow.setText("关注");
            holder.tvFollow.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
            holder.tvFollow.setDrawablePaddingLeft(DensityUtils.dip2px(context, 13));
            holder.tvFollow.setPadding(holder.tvFollow.getPaddingLeft(), holder.tvFollow.getPaddingTop(), DensityUtils.sp2px(context, 13), holder.tvFollow.getPaddingBottom());
            holder.tvFollow.setSelected(false);
            holder.tvFollow.setTextColor(ContextCompat.getColor(context, R.color.white));
        }
        holder.tvFollow.setOnClickListener(new MyOnClickListener(holder, bean, i));

        holder.img_header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent PresenIntent = new Intent(context, PersonalHomeActivity.class);
                PresenIntent.putExtra("homeUserID", bean.getUserId() + "");
                context.startActivity(PresenIntent);
            }
        });
        holder.tv_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent PresenIntent = new Intent(context, PersonalHomeActivity.class);
                PresenIntent.putExtra("homeUserID", bean.getUserId() + "");
                context.startActivity(PresenIntent);
            }
        });

    }


    class MyOnClickListener implements View.OnClickListener {

        CompanysViewHolder holder;
        CompanysBean.CompanysListBean bean;
        int position;

        public MyOnClickListener(CompanysViewHolder holder, CompanysBean.CompanysListBean bean, int position) {
            this.holder = holder;
            this.bean = bean;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            if (SharedPreferencesUtil.isLogin()) {
                listener.setGuanZhu(this.holder, this.bean, this.position);
            } else {
                Intent intent = new Intent(context, LoginPhoneActivity.class);
                intent.putExtra("come_from", "PeiXunFindExpertActivity");
                context.startActivity(intent);
            }
        }
    }


    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    public class CompanysViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_duty)
        TextView tv_duty;
        @BindView(R.id.tv_company_name)
        TextView tv_company_name;
        @BindView(R.id.tv_follow)
        public SuperTextView tvFollow;
        @BindView(R.id.tv_special_auth)
        TextView tv_special_auth;
        @BindView(R.id.img_header)
        ImageView img_header;

        public CompanysViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface CompanysListener {
        void setGuanZhu(CompanysViewHolder holder, CompanysBean.CompanysListBean bean, int position);
    }
}
